/**
 * Created by wangshaohua on 2018/1/14.
 */
moduleLearningMap.controller("LearningMapPdfExerciseDetail", function (LanguageService, $$CambioConfig, UploadFileService, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService, LearningMapExerciseResource, LearningMapH5ExerciseResource) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        //$scope.reset();
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('学习地图网页课程详细'));
        $scope.learningMapExerciseId = $stateParams.learningMapExerciseId;
        $scope.learningMapCourseId = $stateParams.learningMapCourseId;
        $scope.loadDetail();
        $scope.selectTab('tabInfo');
    });

    $scope.currentTabId = 0;
    $scope.selectTab = function (tabId) {
        if (tabId === 'tabInfo') {

            $scope.currentTabId = tabId;
        }
        if (tabId === 'tabExerciseQuickConfig' && $scope.learningMapExerciseId) {
            $scope.currentTabId = tabId;
        }
        if (tabId === 'tabExerciseConfig' && $scope.learningMapExerciseId) {
            $scope.currentTabId = tabId;
        }
    };

    // ------------------------------------------------------------------------------------------------------

    $scope.loadDetail = function () {
        if ($scope.learningMapExerciseId) {
            //LoadingService.show();
            LearningMapExerciseResource.findById({id: $scope.learningMapExerciseId}, {}, function (queryResult) {
                $log.debug("查询结果", queryResult.data);
                $scope.learningMapExerciseDetail = queryResult.data;
                //LoadingService.hide();
            });
        } else {
            $scope.learningMapExerciseDetail = {
                courseId: $scope.learningMapCourseId,
                type: 'H5',
                dynamicHtmlType: 'H5'
            };
        }
    };

    $scope.saveExercise = function () {

        Alert.confirm(function () {
            //LoadingService.show();
            if ($scope.learningMapExerciseId) {
                LearningMapH5ExerciseResource.update({}, $scope.learningMapExerciseDetail, function (queryResult) {
                    $log.debug("查询结果", queryResult.data);
                    //$scope.learningMapCourseDetail = queryResult.data;
                    Alert.success();
                    //LoadingService.hide();
                });
            } else {
                LearningMapH5ExerciseResource.save({}, $scope.learningMapExerciseDetail, function (queryResult) {
                    $log.debug("查询结果", queryResult.data);
                    $scope.learningMapExerciseId = queryResult.data.value;
                    Alert.success();
                    $state.go('sz.learningMapH5ExerciseDetail', {
                        learningMapExerciseId: $scope.learningMapExerciseId,
                        learningMapCourseId: $scope.learningMapCourseId
                    });
                    //LoadingService.hide();
                });
            }
        })
    }

    $scope.cancel = function () {
        DialogService.close();
    };

    // ------------------------------------------------------------------------------------------------------

    $scope.uploadExerciseImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.learningMapExerciseDetail.picture = o.id;
            });
        }
    };

    $scope.uploadFile = function ($files) {
        $scope.fileProgress = {};
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'FILE', $scope).then(function (o) {
                $scope.learningMapExerciseDetail.zip = o.id;
            });
        }
    };

    // ------------------------------------------------------------------------------------------------------
});